const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const license_list = getApp().globalData.license_list
const uav_type_list = getApp().globalData.uav_type_list
const uav_level_list = getApp().globalData.uav_level_list
const pilot_rank_list = getApp().globalData.pilot_rank_list
const about_ins = require('./about_ins')
const about_label = require('./about_label')
// 页面初始加载数据
const page_init=(that, jg_id)=>{
  wx.showLoading({
    title: '页面初始化……',
  })
  wx.request({
    url: domainName + 'create_invitation_init',
    data: {
      jg_id:jg_id
    },...parameter,
    success: res => {
      wx.hideLoading()
      // console.log('res',res.data)
      that.setData({
        'page_data.jg_name':res.data.jg_name,
        'page_data.jg_logo':res.data.jg_logo,
      })

    },
  })
}
// 加载教官列表
const load_label_list=(that,jg_id)=>{
  wx.showLoading()
  wx.request({
    url: domainName + 'create_invitation_load_label_list',
    data: {
      jg_id:jg_id
    },...parameter,
    success: res => {
      wx.hideLoading()
      let label_list = res.data.label_list
      if(label_list.length>0){
        that.setData({
          label_list:res.data.label_list,
          show_page_container:'label'
        })
      }else{
        wx.showLoading({
          title: '您还没有添加学员分组/批次',
        })
      }

    },
  })
}
// 加载标签列表
const load_ins_list=(that, jg_id)=>{
  wx.request({
    url: domainName + 'create_invitation_load_ins_list',
    data: {
      jg_id:jg_id
    },...parameter,
    success: res => {
      let ins_list = res.data.ins_list
      if(ins_list.length>0){
        that.setData({
          ins_list:res.data.ins_list,
          show_page_container:'instructor'
        })
      }else{
        wx.showLoading({
          title: '您还没有添加学员分组/批次',
        })
      }
    },
  })
}
// pages/stu_register_index/create_invitation/create_invitation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    label_list:[],
    ins_list:[],
    license_list:[{id:'',name:'-（学员任意选择）-'}].concat(license_list),
    uav_type_list:[{id:'',name:'-（学员任意选择）-'}].concat(uav_type_list),
    uav_level_list:[{id:'',name:'-（学员任意选择）-'}].concat(uav_level_list),
    pilot_rank_list:[{id:'',name:'-（学员任意选择）-'}].concat(pilot_rank_list),
    page_data : {},    
    // page_data : {
    //   'jg_id' : '', 'jg_name' : '', 'jg_logo' : '', 'label_id' : '', 'ins_id' : '', 'license_id' : '', 'type_id' : '', 'level_id' : '', 'rank_id' : '',
    // }
  },

  // 显示 page_container
  show_page_container(e){
    let tapIndex = e.currentTarget.id
    if(tapIndex==0){
      if(this.data.label_list.length==0){
        load_label_list(this, this.data.page_data.jg_id)
      }else{
        this.setData({
          show_page_container:'label'
        })
      }
    }else if(tapIndex==1){
      if(this.data.ins_list.length==0){
        load_ins_list(this,this.data.page_data.jg_id)
      }else{
        this.setData({
          show_page_container:'instructor'
        })
      }
    }
  },

  cancel(e){
    this.setData({
      show_page_container:false,
    })
  },

  // 学员分组相关
  select_label(e){about_label.select(this, e)},
  clear_label(){about_label.clear(this)},
  add_label(){about_label.add_label(this, this.data.page_data.jg_id)},

  // 分配教官相关
  select_ins(e){about_ins.select(this, e)},
  clear_ins(){about_ins.clear(this)},
  mana_ins(){about_ins.mana_ins(this,this.data.page_data.jg_id)},
  

  // 选择执照种类
  select_license(e){
    let value = e.detail.value
    if(value==0){
      let page_data = this.data.page_data
      delete page_data.license_id
      this.setData({page_data})
    }else{
      this.setData({
        'page_data.license_id':this.data.license_list[value].id,
      })
    }
  },

  // 选择类别等级
  select_type(e){
    let value = e.detail.value
    if(value==0){
      let page_data = this.data.page_data
      delete page_data.type_id
      this.setData({page_data})
    }else{
      this.setData({
        'page_data.type_id':this.data.uav_type_list[value].id
      })
    }
  },

  // 选择级别等级
  select_level(e){
    let value = e.detail.value
    if(value==0){
      let page_data = this.data.page_data
      delete page_data.level_id
      this.setData({page_data})
    }else{
      this.setData({
        'page_data.level_id':this.data.uav_level_list[value].id,
      })
    }
  },

  // 选择操控员等级
  select_pilot(e){
    let value = e.detail.value
    if(value==0){
      let page_data = this.data.page_data
      delete page_data.rank_id
      this.setData({page_data})
    }else{
      this.setData({
        'page_data.rank_id':this.data.pilot_rank_list[value].id,
      })
    }
  },



  // 前往新学员邀请函的预览页面

  preview(){
    // console.log('page_data',page_data)
    wx.navigateTo({
      url:'/pages/students/stu_register/preview_invitation/preview_invitation?page_data=' + encodeURIComponent(JSON.stringify(this.data.page_data)),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let jg_id = options.jg_id
    this.setData({
      'page_data.jg_id':jg_id,
    })
    page_init(this, jg_id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      windowHeight:wx.getSystemInfoSync().windowHeight,
    })
  },


})